<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AdminSettingPanelController extends CAdminController
{
    public $layout='//layouts/admin';

    
    public function actionIndex()
    {
        $model = new SiteSettingForm();
        if(Yii::app()->request->isPostRequest){
            $model->attributes=$_POST['SiteSettingForm'];
            $model->save();
        }
        $this->render('siteSetting',array(
            'model'=>$model, 
        ));
    }

    
    public function actionRetrieveLogo(){
        $model = new SettingViewForm();
        $model->save(array('site_logo'=>'/statics/images/logo.png'));

        $this->redirect(array('index','is_modify'=>true));
    }

    
    public function actionIPad()
    {
        $model = new IpadViewForm();

        if(isset($_POST['IpadViewForm'])){
            $ipadLogoLandscape = CUploadedFile::getInstance($model,'ipadLogoLandscape');
            if (!empty($ipadLogoLandscape)){
                $name = $ipadLogoLandscape->name;
                $ipadLogoLandscapeName = $model->generateFileName($name);
                $_POST['IpadViewForm']['ipadLogoLandscape'] = "/statics/images/".$ipadLogoLandscapeName;
            }

            $ipadLogoPortrait = CUploadedFile::getInstance($model,'ipadLogoPortrait');
            if (!empty($ipadLogoPortrait)){
                $name = $ipadLogoPortrait->name;
                $ipadLogoPortraitName = $model->generateFileName($name);
                $_POST['IpadViewForm']['ipadLogoPortrait'] = "/statics/images/".$ipadLogoPortraitName;
            }

            $model->ipadLogoLandscapeUp = $ipadLogoLandscape;
            $model->ipadLogoPortraitUp    = $ipadLogoPortrait;
            $model->attributes=$_POST['IpadViewForm'];
                        if($model->validate() && $model->save($_POST['IpadViewForm'])){
                                if (!empty($ipadLogoLandscape)){
                    $model->ipadLogoLandscapeUp->saveAs(Yii::app()->basePath."\\..\\statics\\images\\".$ipadLogoLandscapeName);
                }
                if (!empty($ipadLogoPortrait)){
                    $model->ipadLogoPortraitUp->saveAs(Yii::app()->basePath."\\..\\statics\\images\\".$ipadLogoPortraitName);
                }
            }
                    }

        $data = $model->getData();

        $this->render('iPad',array(
            'model'=>$model,
            'data'=>$data,
        ));
    }


    
    public function actionRetrieveIpadLogo(){

        $model = new IpadViewForm();
        $model->save(array('ipadLogoPortrait'=>'/statics/images/ipad-Landscape.png','ipadLogoLandscape'=>'/statics/images/ipad-Portrait.png'));

        $this->redirect(array('iPad'));
    }

    public function actionWindows()
    {
        $this->render('windows');
    }


    
    public function actionIPhone()
    {
        $model = new IphoneViewForm();

        if(isset($_POST['IphoneViewForm'])){
            $iphoneLogoBig = CUploadedFile::getInstance($model,'iphoneLogoBig');
            if (!empty($iphoneLogoBig)){
                $name = $iphoneLogoBig->name;
                $iphoneLogoBigName = $model->generateFileName($name);
                $_POST['IphoneViewForm']['iphoneLogoBig'] = "/statics/images/".$iphoneLogoBigName;
            }

            $iphoneLogoSmall = CUploadedFile::getInstance($model,'iphoneLogoSmall');
            if (!empty($iphoneLogoSmall)){
                $name = $iphoneLogoSmall->name;
                $iphoneLogoSmallName = $model->generateFileName($name);
                $_POST['IphoneViewForm']['iphoneLogoSmall'] = "/statics/images/".$iphoneLogoSmallName;
            }

            $model->iphoneLogoBigUp = $iphoneLogoBig;
            $model->iphoneLogoSmallUp    = $iphoneLogoSmall;
            $model->attributes=$_POST['IphoneViewForm'];
                        if($model->validate() && $model->save($_POST['IphoneViewForm'])){
                                if (!empty($iphoneLogoBig)){
                    $model->iphoneLogoBigUp->saveAs(Yii::app()->basePath."\\..\\statics\\images\\".$iphoneLogoBigName);
                }
                if (!empty($iphoneLogoSmall)){
                    $model->iphoneLogoSmallUp->saveAs(Yii::app()->basePath."\\..\\statics\\images\\".$iphoneLogoSmallName);
                }
            }
                    }

        $data = $model->getData();

        $this->render('iphone',array(
            'model'=>$model,
            'data'=>$data,
        ));
    }


    
    public function actionRetrieveIphoneLogo(){

        $model = new IphoneViewForm();
        $model->save(array('iphoneLogoBig'=>'/statics/images/iPhone-big.png','iphoneLogoSmall'=>'/statics/images/iPhone-small.png'));

        $this->redirect(array('iphone'));
    }

    
    public function actionAndroid()
    {
        $this->render('android');
    }

    
    public function actionUser()
    {
        $registerModel  = new RegisterViewForm();
        $model          = new UserSourceViewForm();
        $modelconnector = new ConnectorViewForm();
        $smtpModel      = new SmtpViewForm();
        $ldapModel      = new LdapViewForm();

        $sourceType = null;
        if (isset($_REQUEST['userSource'])){
            $sourceType = $_REQUEST['userSource'];
        };

        $success = 0;
        $model->rulesUserSource($sourceType);

                if ($sourceType == "owner"){
            $success = 1;
            $model->setOptionValue("userDbSource", $sourceType);
        } elseif ($sourceType == "smtp"){
            $model->setOptionValue("userDbSource", $sourceType);
            if(isset($_POST['SmtpViewForm'])){
                $_POST['SmtpViewForm']['userDbSource'] = $sourceType;
                $smtpModel->attributes=$_POST['SmtpViewForm'];
                                if($smtpModel->validate() && $smtpModel->save($_POST['SmtpViewForm'])){
                    $success = 1;
                }
            }
        }  elseif ($sourceType == "ldap"){
            $model->setOptionValue("userDbSource", $sourceType);
            if(isset($_POST['LdapViewForm'])){
                                $ldapModel->attributes=$_POST['LdapViewForm'];
                                if($ldapModel->save($_POST['LdapViewForm'])){
                    $success = 1;
                }
            }
        } elseif ($sourceType == "connector"){
            if(isset($_POST['ConnectorViewForm'])){
                $_POST['ConnectorViewForm']['userDbSource'] = $sourceType;
                $modelconnector->attributes=$_POST['ConnectorViewForm'];
                                if($modelconnector->validate() && $modelconnector->save($_POST['ConnectorViewForm'])){
                    $success = 1;
                }
            }
        } else {
            if(isset($_POST['RegisterViewForm'])){
                $registerModel->attributes=$_POST['RegisterViewForm'];
                                if($registerModel->validate() && $registerModel->save($_POST['RegisterViewForm'])){
                    $success = 1;
                }
            }

            if(isset($_POST['UserSourceViewForm'])){
                $model->attributes = $_POST['UserSourceViewForm'];
                $model->userSource = $sourceType;
                $model->getDataSource($sourceType);
                                if($model->validate() && $model->save($_POST['UserSourceViewForm'])){
                    $success = 1;
                    if (!empty($sourceType)){
                        $model->setOptionValue("userDbSource", $sourceType);
                    }
                }
            }
        }

        if ($success && $sourceType != "owner"){
            if (SYSTEM_STATUS == "init") {
                SystemManager::addUserSuccess();
            }
        }

        $currentUserDbSource = $model->getUserDbSource();
                if (empty($sourceType)){
            $userDbSource = $currentUserDbSource;
        }else{
            $userDbSource = $sourceType;
        }


        $this->render('user', array(
            'success'              => $success,
            'registerModel'        => $registerModel,
            'model'                => $model,
            'userDbSource'         => $userDbSource,
        	'currentUserDbSource'  => $currentUserDbSource,
            'modelconnector'       => $modelconnector,
            'smtpModel'            => $smtpModel,
            'ldapModel'            => $ldapModel,
        ));
    }


    
    public function actionUrl(){
        $success = 0;
        $model = new UrlViewForm();
        if(isset($_POST['UrlViewForm'])){
            $model->attributes=$_POST['UrlViewForm'];
            if ($model->validate() && $model->save($_POST['UrlViewForm'])){
                unset($_SESSION['appOptions']);                 $success = 1;
            }
        }
        $this->render('url',array(
			"model"=>$model,
            "success"=>$success,
        ));
    }


    
    public function actionEmailServer(){
        $model = new EmailSettingForm();
        if(isset($_POST['EmailSettingForm'])){
            $model->attributes=$_POST['EmailSettingForm'];
            $model->testEmail();
        }
        $this->render('emailSetting',array(
			"model"=>$model,
        ));
    }
}